<?php

namespace app\admin\controller\book;

use think\App;
use app\Request;
use think\facade\Db;
use think\response\Json;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;
use app\admin\model\BookCapitalTurnover;

#[ControllerAnnotation(title: '资金周转')]
class CapitalTurnover extends AdminController
{

    private array $notes;
    private array $rule = [
        'name'   => 'require',
        'date'   => 'require',
        'bill_amount'   => 'require',
        'capital_amount'   => 'require',
        'arrival_amount'   => 'require',
        'service_charge'   => 'require',
        'supplement_amount'   => 'require',
        'supplement_sum'   => 'require',
    ];
    private array $message = [
        'name.require'   => '请输入【周转项目】',
        'date.require'   => '请选择【周转日期】',
        'bill_amount.require'   => '请输入【账单金额】',
        'capital_amount.require'   => '请输入【周转金额】',
        'arrival_amount.require'   => '请输入【到账金额】',
        'service_charge.require'   => '请选择【手续费】',
        'supplement_amount.require'   => '请输入【补充金额】',
        'supplement_sum.require'   => '请输入【补充总计金额】',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        self::$model = new BookCapitalTurnover();
        $notes = self::$model::$notes;

        $this->notes = $notes;
        $this->assign(compact('notes'));
    }

    #[NodeAnnotation(title: '添加', auth: false)]
    public function add(Request $request): string
    {
        if ($request->isPost()) {
            $post  = $request->post();
            $this->validate($post, $this->rule, $this->message);
            $res = $this->rowExist($post['date'], $post['name']);
            if ($res) {
                $this->error("【" . $post['name'] . "】项目" . $post['data'] . "存在周转记录");
            }
            try {
                $save = self::$model::create($post);
            } catch (\Exception $e) {
                $this->error('添加异常');
            }
            if ($save) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    #[NodeAnnotation(title: '列表', auth: true)]
    public function index(Request $request): Json|string
    {
        if ($request->isAjax()) {
            if (input('selectFields')) return $this->selectList();
            list($page, $limit, $where) = $this->buildTableParams();
            $count = self::$model::where($where)->count();
            $list  = self::$model::where($where)->page($page, $limit)->order($this->sort)->select()->toArray();
            $supplementSum = self::$model::where($where)->sum('supplement_sum');
            $serviceCharge = self::$model::where($where)->sum('service_charge');
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
                'totalRow' => [
                    'supplementSum' => $supplementSum,
                    'serviceCharge' => $serviceCharge
                ]
            ];
            return json($data);
        }
        return $this->fetch();
    }

    #[NodeAnnotation(title: '统计', auth: false)]
    public function statistics()
    {
        $supplementSum = self::$model->sum('supplement_sum');
        $serviceCharge = self::$model->sum('service_charge');
        return "累计补充：【" . $supplementSum . "】元，手续费：【" . $serviceCharge . "】元";
    }

    /**
     * 根据周转项目、日期判断是否重复
     * @access public
     * @param string $date 日期
     */
    public function rowExist(string $date, string $name): bool
    {
        $res = BookCapitalTurnover::where(['date' => $date, 'name' => $name])->find();
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }
}
